function selectCheckbox(a) {
    var b = document.getElementById("check-" + a).checked;
    check_children_of(a).each(function() {
        var a = $(this);
        a[0].checked = b, selectCheckbox($(this).parents("tr").attr("id"));
    });
}

function children_of(a) {
    return $("tr.child-of-" + a[0].id);
}

function check_children_of(a) {
    return $(".check-child-of-" + a);
}

function check_parent_of(a) {
    var b = a.substring(9, a.length);
    return $("#check-" + b);
}

function toggle(a) {
    a.is(".collapsed") ? (a.removeClass("collapsed"), a.addClass("expanded"), expand(a)) : (a.removeClass("expanded"), 
    a.addClass("collapsed"), collapse(a));
}

function collapse(a) {
    children_of(a).each(function() {
        var a = $(this);
        collapse(a), a.hide();
    });
}

function expand(a) {
    children_of(a).each(function() {
        var a = $(this);
        a.is(".expanded.parent") && expand(a), a.show();
    });
}

var options;

!function(a) {
    var b = !1;
    a.fn.treeTableRender = function() {
        var b = !0, c = "expanded";
        "false" == a(this).attr("expandable") && (b = !1), "collapsed" == a(this).attr("initState") && (c = "collapsed"), 
        a(this).acts_as_tree_table({
            expandable: b,
            default_state: c
        });
    }, a.fn.acts_as_tree_table = function(c) {
        function e(b) {
            var c, e, f;
            d && (c = a(b.children("td")[options.tree_column]), e = b.attr("id"), f = b.attr("class"), 
            c.prepend('<input type="checkbox" id="check-' + e + '" class="check-' + f + '" style="margin-top:6px" onclick=selectCheckbox("' + e + '")>'));
        }
        function f(b) {
            var e, f, g, h, c = a(b.children("td")[options.tree_column]);
            e = d ? parseInt(c.css("padding-left")) + 2 * options.indent : parseInt(c.css("padding-left")) + options.indent, 
            children_of(b).each(function() {
                a(a(this).children("td")[options.tree_column]).css("padding-left", e + "px");
            }), d && (f = b.attr("id"), g = b.attr("class"), c.prepend('<input type="checkbox" id="check-' + f + '" class="check-' + g + '" style="margin-top:6px" onclick=javascript:selectCheckbox("' + f + '")>')), 
            options.expandable && (c.prepend('<span style="margin-left:0px; padding-left: ' + options.indent + 'px" class="expander"></span>'), 
            h = a(c[0].firstChild), h.click(function() {
                toggle(b);
            }), b.is(".expanded") || b.is(".collapsed") || b.addClass(options.default_state), 
            b.is(".collapsed") ? collapse(b) : b.is(".expanded") && expand(b));
        }
        function g(b) {
            var e, f, g, c = a(b.children("td")[0]);
            d && (e = b.attr("id"), f = b.attr("class"), c.prepend('<input type="checkbox" id="check-' + e + '" class="check-' + f + '" style="margin-top:6px" onclick=javascript:selectCheckbox("' + e + '")>')), 
            c.prepend('<span style="margin-left:0px; padding-left: ' + options.indent + 'px" class="expander"></span>'), 
            g = a(c[0].firstChild), b.addClass("collapsed"), g.click(function() {
                var c = a(this), d = b.attr("url");
                "" != d ? (b.removeClass("collapsed"), b.addClass("table_loading"), window.setTimeout(function() {
                    i(c, d, b);
                }, 200)) : toggle(b);
            });
        }
        function h(b) {
            var g, c = a(b.children("td")[0]);
            d && (b.attr("id"), b.attr("class")), c.prepend('<span style="margin-left:0px; padding-left: ' + options.indent + 'px" class="expander"></span>'), 
            g = a(c[0].firstChild), b.addClass("collapsed"), g.click(function() {
                var c = a(this), d = b.attr("url");
                "" != d ? (b.removeClass("collapsed"), b.addClass("table_loading"), window.setTimeout(function() {
                    i(c, d, b);
                }, 200)) : toggle(b);
            });
        }
        function i(b, c) {
            a.ajax({
                url: c,
                error: function() {
                    try {
                        top.Dialog.alert(uncompile(quiLanguage.table.dataPathErrow));
                    } catch (a) {
                        alert(uncompile(quiLanguage.table.dataPathErrow));
                    }
                },
                success: function(c) {
                    var g, i, e = b.parents("tr"), f = a(c);
                    e.after(f), e.attr("url", ""), e.removeClass("table_loading"), e.addClass("expanded"), 
                    g = e.find("td").eq(0), i = d ? parseInt(g.css("padding-left")) + 2 * options.indent : parseInt(g.css("padding-left")) + options.indent, 
                    children_of(e).each(function() {
                        var b, c, e, f;
                        a(a(this).children("td")[0]).css("padding-left", i + "px"), b = a(this), d && (c = b.attr("id"), 
                        e = b.attr("class"), f = check_parent_of(e), 1 == f.attr("checked") || "true" == f.attr("checked") ? a(a(this).children("td")[0]).prepend('<input checked="true" type="checkbox" id="check-' + c + '" class="check-' + e + '" style="margin-top:6px" onclick=javascript:selectCheckbox("' + c + '")>') : a(a(this).children("td")[0]).prepend('<input type="checkbox" id="check-' + c + '" class="check-' + e + '" style="margin-top:6px" onclick=javascript:selectCheckbox("' + c + '")>')), 
                        b.not(".parent") && null != b.attr("url") && b.addClass("parent"), b.is(".parent") && h(b);
                    });
                }
            });
        }
        var d = !1;
        return options = a.extend({}, a.fn.acts_as_tree_table.defaults, c), this.each(function() {
            var c = a(this);
            b = "ajax" == c.attr("mode") || "true" == c.attr("ajaxMode") || 1 == c.attr("ajaxMode") ? !0 : !1, 
            d = "true" == c.attr("checkMode") || 1 == c.attr("checkMode") ? !0 : !1, c.addClass("acts_as_tree_table"), 
            b ? c.children("tbody").children("tbody tr").each(function() {
                var b = a(this);
                b.not(".parent") && null != b.attr("url") && b.addClass("parent"), b.is(".parent") && g(b);
            }) : c.children("tbody").children("tbody tr").each(function(b) {
                var c = a(this);
                c.not(".parent") && children_of(c).size() > 0 && c.addClass("parent"), c.is(".parent") ? f(c) : 0 != b && e(c);
            });
        });
    }, a.fn.acts_as_tree_table.defaults = {
        expandable: !0,
        default_state: "expanded",
        indent: 19,
        tree_column: 0
    }, a.fn.collapse = function() {
        collapse(this);
    }, a.fn.expand = function() {
        expand(this);
    }, a.fn.toggleBranch = function() {
        toggle(this);
    };
}(jQuery);